package com.lqk.channel.socket.socketchannel;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * 我们可以将 SocketChannel 理解成一个 TCP 客户端
 */
public class SocketChannelDemo {
    public static void main(String[] args) throws Exception {
        //创建SocketChannel
        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress("www.baidu.com", 80));
//        SocketChannel socketChannel2 = SocketChannel.open();
//        socketChannel2.connect(new InetSocketAddress("www.bauidu.com",80));

        //连接校验
//        socketChannel.isOpen();//测试socketChannel是否为open状态
//        socketChannel.isConnected();//测试socketChannel是否已经被连接
//        socketChannel.isConnectionPending();//测试socketChannel是否正在进行连接
//        socketChannel.finishConnect();//校验正在进行套接字连接的socketChannel是否已经完成连接

        //设置非阻塞模式
        socketChannel.configureBlocking(false);

        //读操作
        ByteBuffer byteBuffer=ByteBuffer.allocate(16);
        socketChannel.read(byteBuffer);
        socketChannel.close();
        System.out.println("read over!");

    }
}
